'''
Some common utilities
'''

import sys
import subprocess

# from post-review
def execute(command, split_lines=False, ignore_errors=False,
            extra_ignore_errors=()):
    """
    Utility function to execute a command and return the output.
    """
    if sys.platform.startswith('win'):
        p = subprocess.Popen(command,
                             stdin=subprocess.PIPE,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.STDOUT,
                             shell=True,
                             universal_newlines=True)
    else:
        p = subprocess.Popen(command,
                             stdin=subprocess.PIPE,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.STDOUT,
                             shell=True,
                             close_fds=True,
                             universal_newlines=True)
    if split_lines:
        data = p.stdout.readlines()
    else:
        data = p.stdout.read()
    rc = p.wait()
    if rc and not ignore_errors and rc not in extra_ignore_errors:
        print 'Failed to execute command: %s\n%s' % (command, data)
        exit(1)

    return data